#!/usr/bin/env python

from PyQt4.QtGui import *
from PyQt4.QtCore import *
from Ui_dialog_new_relation import *
#from EA import *

class dialog_new_relation(QtGui.QDialog, Ui_Dialog_new_relation):
    
    def __init__(self,parent_window,AE, relation_type_id, libmyCF, option):
        super(dialog_new_relation,self).__init__(parent_window)
        
        # Options :
        # if option is None, the dialog allows to choose both child and parent
        # if option == 0 : dialog will be in mode "add child only"
        # if option == 1 : dialog will be in mode "add parent only"
        
        self.setupUi(self)

        self.AE_preselected = AE #in default mode, the preselected AE is the parent AE
                                 #in mode "add child", the preselected AE is the parent AE
                                 #in mode "add parent", the preselected AE is the child AE
        self.relation_type_id = relation_type_id
        self.libmyCF = libmyCF
        self.default_mode = True
        self.child_mode = False #In this mode you can not change the parent AE, it is preselected
        self.parent_mode = False #In this mode you can not change the Child AE, it is preselected
        
        if option == 0 :
            self.child_mode = True
            self.default_mode = False
        if option == 1 :
            self.parent_mode = True
            self.default_mode = False
   
        self.connect(self.buttonBox, SIGNAL("accepted()"),self, SLOT("accept()"))
        self.connect(self.buttonBox, SIGNAL("rejected()"),self, SLOT("reject()"))
      
        # Set the list of relation_type in combobox_relationtype    
        self.comboBox_relationtype.clear()
        for relation_type_key in self.libmyCF.ontology.all_relations_type:  
            r = self.libmyCF.ontology.all_relations_type[relation_type_key]
            self.comboBox_relationtype.addItem(QString.fromUtf8(r.name), QVariant(r.relation_type_id))
        
        index = self.comboBox_relationtype.findData(QVariant(self.relation_type_id))
        self.comboBox_relationtype.setCurrentIndex(index)
                 
       
        # Set the list of AE in the combobox_parent and combobox_child
        
        self.comboBox_child.clear()
        self.comboBox_parent.clear()
        self.dict_AE = dict() #dict_AE = {('name_AE1':AE_id1),('name_AE2':AE_id2),...}
        self.names_AE =[] #names_AE = list of all the names of the AEs 
        for k in self.libmyCF.ontology.all_AEs:
            self.dict_AE[self.libmyCF.ontology.all_AEs[k].name]=self.libmyCF.ontology.all_AEs[k].AE_id
            self.names_AE.append(self.libmyCF.ontology.all_AEs[k].name)
        self.names_AE.sort() 
        
        #default mode and child mode
        if self.child_mode or self.default_mode :
            self.comboBox_child.addItem ( QString.fromUtf8("Select child..."))
            for name in self.names_AE:
                self.comboBox_child.addItem ( QString.fromUtf8(name),QVariant(self.dict_AE.get(name)))
                self.comboBox_parent.addItem ( QString.fromUtf8(name),QVariant(self.dict_AE.get(name)))
            index = self.comboBox_parent.findText(QString(self.AE_preselected.name))
            self.comboBox_parent.setCurrentIndex(index)
            if self.child_mode:
                index = self.comboBox_parent.findText(QString(self.AE_preselected.name))
                self.comboBox_parent.setCurrentIndex(index)
                self.comboBox_parent.setEnabled(False)
            
        #parent mode
        if self.parent_mode :
            self.comboBox_parent.addItem(QString.fromUtf8("Select parent..."))
            for name in self.names_AE:
                self.comboBox_child.addItem ( QString.fromUtf8(name),QVariant(self.dict_AE.get(name)))
                self.comboBox_parent.addItem ( QString.fromUtf8(name),QVariant(self.dict_AE.get(name)))
            index = self.comboBox_child.findText(QString(self.AE_preselected.name))
            self.comboBox_child.setCurrentIndex(index)
            self.comboBox_child.setEnabled(False)
        
    
    def accept(self):
        # child and parent are tuples with the name and the AE_id of the selected AE
        self.child = ( str(self.comboBox_child.currentText()), self.comboBox_child.itemData(self.comboBox_child.currentIndex()).toInt()[0])
        self.parent = ( str(self.comboBox_parent.currentText()), self.comboBox_parent.itemData(self.comboBox_parent.currentIndex()).toInt()[0])
        
        # get the id of the selected relation_type
        self.selected_relation = self.comboBox_relationtype.itemData(self.comboBox_relationtype.currentIndex()).toInt()[0]
       
        #Verify the entered relation is right
        if self.comboBox_child.currentText()== "Select child...":
            QMessageBox.information(self,"myCF information","Please select a child.")
            return
        if self.comboBox_parent.currentText()== "Select parent...":
            QMessageBox.information(self,"myCF information","Please select a parent.")
            return
        if self.parent == self.child :
            QMessageBox.information(self,"myCF information","Parent and child are the same entity.\nRelation will not be created")
            return
        if not self.names_AE.count(self.child[0])== 1 :
            QMessageBox.warning(self,"myCF","There are several AE with the name \'"+str(self.child[0])+"\'\nTo prevent from errors, relation will not be added. \nPlease rename those AE.")
            return
        if not self.names_AE.count(self.parent[0])== 1 :
            QMessageBox.warning(self,"myCF","There are several AE with the name \'"+str(self.parent[0])+"\'\nTo prevent from errors, relation will not be added. \nPlease rename those AE.")
            return
        if self.selected_relation == self.libmyCF.ontology.getRelationTypeNamed( "is_a").getId() and self.libmyCF.ontology.has_parent(self.child[1],self.selected_relation):
            QMessageBox.information(self,"myCF information","The selected AE already has one parent for the selected relation type.\nRelation will not be created")
            return
        #self.ontology.add_relation(self.child[1],self.parent[1],self.selected_relation)   
        QtGui.QDialog.accept(self)
         